package com.beerandjesus.sportofkings.handicapper.migration.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import java.util.List;

/**
 * Date: 3/20/15
 * Time: 9:57 PM
 */
public class Migrator {

    private SessionFactory sessionFactoryFrom;

    private SessionFactory sessionFactoryTo;

    private HibernateCopier hibernateCopier;

    private List<Class<?>> entityClasses;

    public Migrator(SessionFactory sessionFactoryFrom,
                    SessionFactory sessionFactoryTo,
                    HibernateCopier hibernateCopier,
                    List<Class<?>> entityClasses) {
        this.sessionFactoryFrom = sessionFactoryFrom;
        this.sessionFactoryTo = sessionFactoryTo;
        this.hibernateCopier = hibernateCopier;
        this.entityClasses = entityClasses;
    }

    public void migrate() {
        Session sessionFrom = sessionFactoryFrom.openSession();
        Session sessionTo = sessionFactoryTo.openSession();
        for (Class<?> clazz : entityClasses) {
            hibernateCopier.copy(clazz, sessionFrom, sessionTo);
        }
        sessionFrom.close();
        sessionTo.close();
    }

}
